﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace XMM.Dialoges.Library
{
    /// <summary>
    /// Interaktionslogik für AddShowSeason.xaml
    /// </summary>
    public partial class AddShowSeason : Window
    {
        private Controls.Library.Import_Episode ParentEpisodeImporter;

        private Objects.Series SelectedShow;

        public bool PendingChanges
        {
            get
            {
                return _PendingChanges;
            }
        }

        private bool _PendingChanges;

        public AddShowSeason(Controls.Library.Import_Episode ctlParentEpisodeImporter)
        {
            InitializeComponent();

            ParentEpisodeImporter = ctlParentEpisodeImporter;
            SelectedShow = (Objects.Series)ParentEpisodeImporter.cmbSeries.SelectedItem;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Get Show Name.
            lblShowName.Content = SelectedShow.Name;

            // Get next Season's Number.
            txtSeasonNo.Text = (ParentEpisodeImporter._AvailableSeasons.Count + 1).ToString();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (txtSeasonNo.Text != String.Empty)
            {
                int iSeasonNo = 0;

                try
                {
                    iSeasonNo = int.Parse(txtSeasonNo.Text);
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show("Bitte geben Sie für die Staffel-Nr. eine Zahl oder Ziffer ein! Buchstaben oder Sonderzeichen sind nicht erlaubt.", "Fehler in Staffel-Nr.", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }

                if (iSeasonNo > 0)
                {
                    Objects.SeriesSeason oSeason = new Objects.SeriesSeason();
                    oSeason.Number = iSeasonNo;
                    oSeason.Complete = false;
                    if ((bool)chkComplete.IsChecked)
                        oSeason.Complete = true;
                    if ((bool)chkComplete.IsChecked)
                        oSeason.DateComplete = (DateTime)dtpDateCompleted.SelectedDate;
                    oSeason.SeriesID = SelectedShow.SeriesID;
                    oSeason.BaseDirectory = txtBaseDir.Text;

                    if (oSeason.SaveNew())
                    {
                        ParentEpisodeImporter.loadSeasons();
                        Close();
                    }
                }
                else
                {
                    System.Windows.MessageBox.Show("Die Staffel-Nr. muss größer als 0 (Null) sein!", "Staffel-Nr. ungültig", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }
            }
            else
            {
                System.Windows.MessageBox.Show("Bitte geben Sie die Staffel-Nr. an!", "Staffel-Nr. fehlt", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            if (PendingChanges)
            {
                if (System.Windows.MessageBox.Show("Ihre Eingaben werden verworfen! Möchten Sie wirklich abbrechen?", "Wirklich abbrechen", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
                {
                    return;
                }
            }

            Close();
        }

        private void chkComplete_Checked(object sender, RoutedEventArgs e)
        {
            chkComplete.Content = "JA";
            dtpDateCompleted.IsEnabled = true;
        }

        private void chkComplete_Unchecked(object sender, RoutedEventArgs e)
        {
            chkComplete.Content = "Nein";
            dtpDateCompleted.IsEnabled = false;
        }

        private void btnBrowseBaseDir_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog dlgBrowseTarget = new FolderBrowserDialog();
            dlgBrowseTarget.ShowNewFolderButton = true;
            if (dlgBrowseTarget.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtBaseDir.Text = dlgBrowseTarget.SelectedPath;
            }
        }
    }
}
